﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.Serialization;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            // 服务器基址   
            Uri baseAddress = new Uri("http://localhost:1378/services");
            // 声明服务器主机   
            using (ServiceHost host = new ServiceHost(typeof(MyService), baseAddress))
            {
                // 添加绑定和终结点   
                WSHttpBinding binding = new WSHttpBinding();
                host.AddServiceEndpoint(typeof(IService), binding, "/test");
                //// 添加服务描述   
                //host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
                //// 把自定义的IEndPointBehavior插入到终结点中   
                //foreach (ServiceEndpoint endpont in host.Description.Endpoints)
                //{
                //    endpont.Behaviors.Add(new Inspector.MyEndPointBehavior());
                //}
                try
                {
                    // 打开服务   
                    host.Open();
                    Console.WriteLine("服务已启动。");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadKey();
            }
        }
    }

    [ServiceContract(Namespace = "http://Vanglen")]
    public interface IService
    {
        [OperationContract(IsOneWay = true)]
        void Add(double n1, double n2);
        [OperationContract]
        int AddInt(int a, int b);
        [OperationContract]
        Student GetStudent();
        [OperationContract]
        CalResultResponse ComputingNumbers(CalcultRequest inMsg);
    }

    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class MyService : IService
    {
        public int AddInt(int a, int b)
        {
            return a + b;
        }

        public Student GetStudent()
        {
            Student stu = new Student();
            stu.StudentName = "小明";
            stu.StudentAge = 22;
            return stu;
        }

        public CalResultResponse ComputingNumbers(CalcultRequest inMsg)
        {
            CalResultResponse rmsg = new CalResultResponse();
            switch (inMsg.Operation)
            {
                case "加":
                    rmsg.ComputedResult = inMsg.NumberA + inMsg.NumberB;
                    break;
                case "减":
                    rmsg.ComputedResult = inMsg.NumberA - inMsg.NumberB;
                    break;
                case "乘":
                    rmsg.ComputedResult = inMsg.NumberA * inMsg.NumberB;
                    break;
                case "除":
                    rmsg.ComputedResult = inMsg.NumberA / inMsg.NumberB;
                    break;
                default:
                    throw new ArgumentException("运算操作只允许加、减、乘、除。");
                //break;
            }
            return rmsg;
        }

        public void Add(double n1, double n2)
        {
            //
        }
    }

    [DataContract]
    public class Student
    {
        [DataMember]
        public string StudentName;
        [DataMember]
        public int StudentAge;
    }

    [MessageContract]
    public class CalcultRequest
    {
        [MessageHeader]
        public string Operation;
        [MessageBodyMember]
        public int NumberA;
        [MessageBodyMember]
        public int NumberB;
    }

    [MessageContract]
    public class CalResultResponse
    {
        [MessageBodyMember]
        public int ComputedResult;
    }
}
